﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Achievement.BLL.Provider;
using Achievement.DataModels;
using Achievement.Common;
using Achievement.Web.Common;

namespace Achievement.Web.Controllers
{
    public class LogOnController : Controller
    {
        //
        // GET: /LogOn/
        [HttpGet]
        public ActionResult Index()
        {
            var baseCount = UsersProvider.Instance.GetUserBaseCount();
            ViewData["UserCount"] = baseCount + UserInfo.GetCount();
            return View();
        }

        [HttpPost]
        public ActionResult Login(string username,string password,string guest)
        {
            if (string.IsNullOrEmpty(guest))
            {
                Users u = UsersProvider.Instance.GetUserByLoginName(username);
                if (ATools.MD5Encrypt(password) == u.PassWord)
                {
                    if (u.IsActive == true)
                    {
                        FormsAuthentication.SetAuthCookie(username, false);
                        return Redirect("/");
                    }
                    else
                    {
                        return Content(ATools.OperateAlter("用户尚未审核通过，请耐心等待！", "/Logon"));
                    }
                }
                return Content(ATools.OperateAlter("用户名或密码错误！", "/Logon"));
            }
            else
            {
                FormsAuthentication.SetAuthCookie("guest", false);
                return Redirect("/");
            }
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return Redirect("/Logon");
        }
    }
}
